from constant import *


class Sun(MovableGifImageObject):
    def __init__(self, screen: pg.Surface):
        super().__init__(screen, 'images\\interface\\Sun.gif')
        coordinate = randint(0, LAWNS_WIDTH) + LAWN_START_X, 0
        self.trajectory = self.points_trajectory(coordinate, (coordinate[0], LAWNS_HEIGHT), step=8)
        self.disappear_cycle_number = randint(SUN_DISAPPEAR_CYCLE[0], SUN_DISAPPEAR_CYCLE[1])
        self.disappear = False

    def onclick(self, mouse_pos):
        if self.is_in(mouse_pos):
            self.trajectory = self.points_trajectory(self.coordinate, (0, 0), step=30)
            return True
        else:
            return False


class SunBack:
    def __init__(self, screen: pg.Surface):
        self.screen = screen
        self.frame_sur = pg.image.load('images\\interface\\SunBack.png')
        self.sun_count = 0
        self.font = pg.font.Font('font\\font3.ttf', 40)

    def update(self):
        sun_text = self.font.render(f'{self.sun_count}', True, 'black')
        self.screen.blit(self.frame_sur, (0, 0))
        self.screen.blit(sun_text, (100, 10))


class FlagMeter:
    def __init__(self, screen: pg.Surface, waves_zombies: list[int]):
        self.screen = screen
        self.surface = pg.Surface(FLAG_METER_SIZE, pg.SRCALPHA)
        self.coordinate = (SCREEN_WIDTH - FLAG_METER_SIZE[0], SCREEN_HEIGHT - FLAG_METER_SIZE[1])
        self.empty = pg.image.load('images\\interface\\FlagMeterEmpty.png')
        self.full = pg.image.load('images\\interface\\FlagMeterFull.png')
        self.parts1 = pg.image.load('images\\interface\\FlagMeterParts1.png')
        self.parts2 = pg.image.load('images\\interface\\FlagMeterParts2.png')
        self.waves_zombies = waves_zombies
        self.now_zombies = -1
        self.waves = len(waves_zombies)
        self.wave_length = FLAG_LENGTH / self.waves

    def update(self, wave: int, now_zombies: int):
        """
        :param wave: 波次，从0开始
        :param now_zombies: 现在这个波次出现的僵尸数量
        :return: None
        """
        surplus = now_zombies / self.waves_zombies[wave]
        surface = pg.Surface(((self.waves - wave - surplus) * self.wave_length + 10, FLAG_METER_SIZE[1]), pg.SRCALPHA)
        surface.blit(self.empty, (10, 25))
        for number in range(self.waves):
            x = FLAG_LENGTH - (number + 1) * self.wave_length + 16
            if number < wave or (number == wave and surplus == 1.0):
                y = 8
            else:
                y = 10
            self.surface.blit(self.parts2, (x, y))
        self.surface.blit(self.full, (10, 25))
        self.surface.blit(self.parts1, (((self.waves - wave - surplus) * self.wave_length) + 10, 25))
        self.surface.blit(surface, (0, 0))
        self.screen.blit(self.surface, self.coordinate)

